//
//  TMMovieInfoVC.m
//  movieInfo
//
//  Created by zhangkai on 2023/6/3.
//

#import "TMMovieInfoVC.h"
#import <Masonry.h>
#import "TMTVInfoCell.h"
#import "movieInfo-Swift.h"
#import "MoiveinfoModel.h"
#import "movieInfo-Swift.h"
#import "TMMovieModel.h"
#import "TMMovieInfoModel.h"
#import "TMAllMovieListView.h"

@interface TMMovieInfoVC ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic)UITableView *mainTableView;


@property(strong,nonatomic)TMMovieInfoModel *model;

@property(strong,nonatomic)TMTVInfoPlayView *playView;


@property(strong,nonatomic)UIButton *shawView;
@property(strong,nonatomic)TMAllMovieListView *allView;

//

@end

@implementation TMMovieInfoVC


- (void)configTopView{
    
    
//https://autoeq.top/mov/a00d4e8d2c0e9f0a8bb202f951fb4204fix5/a00d4e8d2c0e9f0a8bb202f951fb4204fix5.m3u8?expire=1682874363&auth=102af7c16f4cec35c1f01c0d08b5bc64a1019077689ef7179fdc1bd14f741f53&xaid=AEBA122D-1EB8-4893-B09F-B6A146384041&p1=35140,265,169.150.221.195
//
    
    UIView *topView = [UIView new];
    topView.frame = CGRectMake(0, 0, kScreenW, 100);
    topView.backgroundColor = [UIColor blackColor];
    self.mainTableView.tableHeaderView = topView;
    

    
    
//    VKVikiVideoPlayer *player = [[VKVikiVideoPlayer alloc] initWithVideoPlayerView:[[VKVikiVideoPlayer alloc] init]];
//    self.player.delegate = self;
//    [self.view addSubview:self.player]
    
    UILabel *nameLabel = ({
        UILabel *nameLabel = [UILabel new];
        nameLabel.text = @"the fallout";
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.font = [UIFont systemFontOfSize:24 weight:UIFontWeightMedium];
        nameLabel;
    });
    [topView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView).offset(15);
        make.left.equalTo(topView).offset(15);
    }];
    
    UIImageView *xingIcon = ({
        UIImageView *icon = [UIImageView new];
        icon.image = [UIImage imageNamed:@"star"];
        icon;
    });
    [topView addSubview:xingIcon];
    [xingIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(nameLabel);
        make.right.equalTo(topView).offset(-20);
    }];
    
    UIImageView *shareIcon = ({
        UIImageView *icon = [UIImageView new];
        icon.image = [UIImage imageNamed:@"star"];
        icon;
    });
    [topView addSubview:shareIcon];
    [shareIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(nameLabel);
        make.right.equalTo(xingIcon.mas_left).offset(-20);
    }];
    
    UIButton *fenBtn = ({
        UIButton *btn = [UIButton new];
        [btn setTitle:@"IMDB：9.8" forState:UIControlStateNormal];
        btn.backgroundColor = Kcolor(255, 109, 109, 0.1);
        [btn setTitleColor:Kcolor(255, 109, 109, 1) forState:UIControlStateNormal];
        btn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.userInteractionEnabled = NO;
        btn;
    });
    
    [topView addSubview:fenBtn];
    [fenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topView).offset(15);
        make.bottom.equalTo(topView).offset(-20);
    }];
    
    UIButton *timeBtn = ({
        UIButton *btn = [UIButton new];
        [btn setTitle:@"2022-01-28" forState:UIControlStateNormal];
        btn.backgroundColor = Kcolor(39, 58, 228, 0.1);
        [btn setTitleColor:Kcolor(39, 58, 228, 1) forState:UIControlStateNormal];
        btn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.userInteractionEnabled = NO;
        btn;
    });
    
    [topView addSubview:timeBtn];
    [timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(fenBtn.mas_right).offset(15);
        make.bottom.equalTo(topView).offset(-20);
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:13/255.0 green:19/255.0 blue:35/255.0 alpha:1];
    
    UIView *topView = [UIView new];
    topView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.right.equalTo(self.view);
        make.height.mas_equalTo(211);
    }];
    
//    TMTVInfoPlayView TMTVInfoPlayVC
    
    TMTVInfoPlayView *playView = [TMTVInfoPlayView new];
//    [topView addSubview:playView];
//    playView.frame = CGRectMake(0, 0, kScreenW, 211);
    [self addChildViewController:playView];
    [topView addSubview:playView.view];
    playView.view.frame = CGRectMake(0, 0, kScreenW, 211);
    [playView didMoveToParentViewController:self];
    self.playView = playView;
    
    

    self.mainTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.rowHeight = 260;
    self.mainTableView.estimatedSectionFooterHeight = 1;

    self.mainTableView.estimatedSectionHeaderHeight = 40;
    [self.view addSubview:self.mainTableView];
    [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(topView.mas_bottom);
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
    self.mainTableView.backgroundColor = [UIColor colorWithRed:13/255.0 green:19/255.0 blue:35/255.0 alpha:1];


    
    self.shawView = [UIButton new];
    self.shawView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.6];
    [self.shawView addTarget:self action:@selector(clickdiss) forControlEvents:UIControlEventTouchUpInside];
    self.shawView.hidden = YES;
    [self.view addSubview:self.shawView];
    [self.shawView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom);
        make.height.mas_equalTo(kScreenH);
    }];
    
    self.allView = [[TMAllMovieListView alloc] initWithFrame:CGRectMake(0, kScreenH-450, kScreenW, 450)];
    [self.view addSubview:self.allView];
    self.allView.hidden = YES;

    [self configTopView];
    
    [self getInfo];
//    [self getdataInfo];
}

- (void)clickdiss{
    self.shawView.hidden = YES;
    self.allView.hidden = YES;
}










- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; {
    return  self.model.categoryArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
    return 1;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;{
    TMTVInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TMTVInfoCell"];
    if (!cell) {
        cell = [[TMTVInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TMTVInfoCell"];
    }
    [cell configDic:self.model.categoryArr[indexPath.section]];
    
    return  cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    NSDictionary *dic = self.model.categoryArr[section];
    
    UIView *bgView = [UIView new];
    bgView.frame = CGRectMake(0, 0, 0, 40);
    bgView.backgroundColor = [UIColor colorWithRed:13/255.0 green:19/255.0 blue:35/255.0 alpha:1];
    
    
    UILabel *la = [UILabel new];
    la.text = [dic objectForKey:@"name"];
    la.textColor = [UIColor whiteColor];
    la.font = [UIFont systemFontOfSize:14];
    [bgView addSubview:la];
    [la mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bgView);
        make.left.equalTo(bgView.mas_left).offset(15);
    }];
    
    NSString *idstr = [dic objectForKey:@"47"];
    UIButton *btn = [UIButton new];
    btn.tag = section;
    [btn setTitle:@"More >" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bgView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bgView);
        make.right.equalTo(bgView.mas_right).offset(-15);
    }];
    if([idstr isEqualToString:@"0"]){
        btn.hidden = YES;
    }else{
        btn.hidden = NO;
    }
    [btn addTarget:self action:@selector(clickMore:) forControlEvents:UIControlEventTouchUpInside];
    return bgView;
    
}
- (void)clickMore:(UIButton *)sender{
    NSDictionary *dic = self.model.categoryArr[sender.tag];
    
    self.allView.hidden = NO;
    self.shawView.hidden = NO;
    [self.allView configWithID:[dic objectForKey:@"47"]];
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}


- (void)getInfo{
    
    NSTimeInterval timein = [[NSDate date] timeIntervalSince1970];
    int intTime = (int)timein;
    NSString *timeStr = [NSString stringWithFormat:@"%d",intTime];
    
    NSString *noStr = [[TMDeviceInfo deviceNo] stringByReplacingOccurrencesOfString:@"-" withString:@""];
    
    NSString *uavalie = [[TMNetworkManage defaultManager] getencry:[NSString stringWithFormat:@"%@%@",noStr,timeStr]];
    NSString *sigs = [[TMNetworkManage defaultManager] getsigencry:uavalie];
    
    
    
//    let devNo = DeviceInfo.deviceNo.replacingOccurrences(of: "-", with: "")
//    let ueValue = EncryManager.sharedInstance.loadDevEncry(value: devNo+time)
//    let sig = EncryManager.sharedInstance.loadSigEncryManager(value:ueValue)
    
    if(self.idstr.length == 0){
        return;
    }
    
    NSDictionary *dic = @{
        @"id":self.idstr,
        @"api_ver":@"1",
        @"reg_id":@" ",
        @"sig":sigs,
        @"unixtime":timeStr
    };
    //
    
    [[TMNetworkManage defaultManager] MVWithApiName:@"144/" parameters:dic success:^(NSURLSessionDataTask * _Nonnull dataTask, id  _Nonnull responseObject) {
        
        TMMovieModel *model = [TMMovieModel mj_objectWithKeyValues:responseObject];
        if(model.state == 200){
            NSString *time = [model.allDic objectForKey:@"168"];
            NSString *tagstr = [model.allDic objectForKey:@"356"];
            NSMutableArray *tags = [[tagstr componentsSeparatedByString:@","] mutableCopy];
            [tags addObject:time];
            
            TMMovieInfoModel *infoModel = [TMMovieInfoModel mj_objectWithKeyValues:model.allDic];
            infoModel.curtags = tags.copy;
            
            NSMutableArray *newlistArr = @[].mutableCopy;
            for (NSDictionary *dic1 in model.categArr) {
                if([[dic1 objectForKey:@"name"] length] > 0){
                    [newlistArr addObject:dic1];
                }
            }
            
            infoModel.categoryArr = newlistArr.copy;
            
//            NSArray *arr = [model.allDic objectForKey:@"236"];
            
  
            
            if(infoModel.playUrl.length == 0){
                infoModel.playUrl = [[model.allDic objectForKey:@"hd"] objectForKey:@"link"];
            }
            
            if(infoModel.playUrl.length == 0){
                infoModel.playUrl = [[model.allDic objectForKey:@"sd"] objectForKey:@"link"];
            }
            [PlayerManage defaultManager].currentMovie = infoModel;
            self.model = infoModel;
            [self.mainTableView reloadData];
            
//            if(infoModel.playUrl.length > 0){
//                [self.playView setupPlayerResourceWithPlaystr:infoModel.playUrl];
//            }
//
            
            
            
            
        }
        
                
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
        
    }];
    
    
    
}


- (void)getdataInfo{
    
    
    NSDictionary *dic = @{
        @"id":self.idstr,
        @"type":@"1",
        @"reg_id":@" ",
    };
    
    
    
    
    [[TMNetworkManage defaultManager] MVWithApiName:@"163/" parameters:dic success:^(NSURLSessionDataTask * _Nonnull dataTask, id  _Nonnull responseObject) {
        
        TMMovieModel *model = [TMMovieModel mj_objectWithKeyValues:responseObject];
        if(model.state == 200){
            
            NSArray *arr = [model.allDic objectForKey:@"subtitle"];
            NSMutableArray *muArr = @[].mutableCopy;
            for (NSDictionary *dic in arr) {
                TMMovieModel *tempModel = [TMMovieModel new];
                tempModel.sub = [dic objectForKey:@"sub"];
                tempModel.lang = [dic objectForKey:@"178"];
                [muArr addObject:tempModel];
            }
            
//            if(![PlayerManage defaultManager].currentMovie.subtitleArr){
//                [PlayerManage defaultManager].currentMovie.subtitleArr = @[].copy;
//            }
            [PlayerManage defaultManager].currentMovie.subtitleArr = muArr.copy;
            
//           NSString *playstr =  [[PlayerManage defaultManager] playUrl];
//
//            if(playstr.length > 0){
//                [self.playView setupPlayerResourceWithPlaystr:playstr];
//            }
//

        }
        
                
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
        
    }];
    
    

}
@end
